// 数据是假的
import mockRout from '../utils/navJson.json'
import _import from './_import.js'
import {menuStore} from '@/stores/menu.js'
import {getMenu} from "@/server/common/login.js"

// 处理路由数据
class initRouter {
  constructor(list){
    this.list = list
    this.routes = []
    this.fullRout(this.list)
  }
  getRoutes() {
    return this.routes
  }
  fullRout(list=[]){
    list.forEach(item => {
      if(item.url){
        this.routes.push({path: item.url, name: "name+"+item.menuId, component: _import(item.url), meta: {title: item.name}})
      }else if(item.list?.length>0){
        this.fullRout(item.list)
      }
    })
  }
  getAllMenu() {
    return this.list
  }
}

// 生成路由数据
const apiRoutes = () => {
  return new Promise((resolve, reject) => {
    // 接口拿到数据后 处理路由数据 处理菜单数据 并将菜单数据存菠萝 
    getMenu().then(res => {
        const routes = new initRouter(res.menuList[0]?.list||[])
        const allMenu = routes.getAllMenu()
        menuStore().setAllMenu(allMenu)
        resolve(routes.getRoutes())
    })
  })
} 


export {apiRoutes}
